home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / DATA_MG.ZIP / QTEST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-30  |  2.7 KB  |  82 lines

  1. /**
  2.    QTEST.C - This program is a sample to test the Queue functions.
  3.          It is a very simple program used to demonstrate the use of queues
  4.          (circular) in communicating with hardware.  The basic Idea of this
  5.          Program is that the user types anything at the keyboard and it is
  6.          echoed back to them, pressing ESC ends the program.  If they press
  7.          CNTRL-A, then a string is inserted into the Queue, called TextBuffer.
  8.          When the function MyGetCh() is called, if the user has not pressed
  9.          a key, the Service() function is called which checks the Queue to 
  10.          see if any data is available, if so one character is removed and
  11.          printed to the screen or printer (based on the value of ReallyPrint).
  12.          The variable SLOWFACTOR can be incremented or decremented to slow
  13.          down or speed up the printing process.
  14.          To test the program, type in some text, and then press CNTRL-A, the
  15.          QuePrint function inserts text into the buffer, and while the program
  16.          is waiting for the next keystroke, it is also printing information
  17.          from the buffer. Press CNTRL-A, and then several other keys quickly
  18.          after to see you typed-in text mixed with the 'Queued' text.
  19.  
  20. To Compile:  TCC or QCL QTEST.C Q.OBJ       (See Q.C for info to make Q.OBJ)
  21. Mario Giannini
  22. **/
  23. #include <stdio.h>
  24. #include "q.h"
  25.  
  26. int ReallyPrint=0;   /* Change to one and Que buffer goes to printer */
  27. int SLOWFACTOR=200;  /* Slows down Que output to be visible (on a '386 anyway) */
  28.  
  29. main()
  30. {
  31.     Que *TextBuffer;
  32.     int i;
  33.  
  34.     TextBuffer=CreateQ(2048);
  35.     Service(TextBuffer);
  36.  
  37.     printf("Type in any text, and Ctrl-A To place text into the Que\n");
  38.  
  39.     while( (i=MyGetCh())!=27)
  40.     {
  41.         if(i==1)
  42.             QuePrint(TextBuffer, "\nThis is the sample text to insert into the Que\n");
  43.         else
  44.             printf("%c", i);
  45.     }
  46.     DestroyQ(TextBuffer);
  47. }
  48.  
  49.  
  50. MyGetCh()  /* getch replacement that calls Service() while idle */
  51. {
  52.     while(!kbhit())
  53.         Service(NULL);
  54.     return(getch());
  55. }
  56.  
  57. /* The Service func, prints a character from Que to printer or screen */
  58. Service(Que *Q2Register) 
  59. {
  60.     static Que *MyLocalQue=NULL;
  61.     static int DoNothing=0;
  62.  
  63.     if(Q2Register!=NULL)
  64.         MyLocalQue=Q2Register;
  65.     if(MyLocalQue!=NULL && !Empty(MyLocalQue))
  66.     {
  67.         if(SLOWFACTOR && (DoNothing++%SLOWFACTOR))  
  68.             return;  /* If func returns 9 out of 10 times and does nothing, result are more noticeable */
  69.         if(ReallyPrint)
  70.             fprintf(stdprn, "%c", GetFromQ(MyLocalQue));
  71.         else
  72.             printf("%c", GetFromQ(MyLocalQue));
  73.     }
  74. }
  75.  
  76. QuePrint(Que *Q2Print2, char *Str2Add) /* Copies a string into the Que */
  77. {
  78.     while(*Str2Add)
  79.         if(InsertIntoQ(Q2Print2, *Str2Add++)!=ALLOK)
  80.             break;
  81. }
  82.